﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmConfirmSupervisionCode
    /// Version:1.0
    /// Date:2024-06-26
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现扫码确定电监码
    /// </remarks>    
    public partial class frmConfirmSupervisionCode : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        public string m_strSupervisionCode = ""; // 电监码
        public Boolean m_bIsRefresh = false; // 是否刷新

        #endregion
        public frmConfirmSupervisionCode()
        {
            InitializeComponent();
        }

        #region 02.自定义方法
        #endregion        

        #region 03.系统事件方法

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void txtSupervisionCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                _init();
                txtSupervisionCode0.Text = txtSupervisionCode.Text.Trim();
                if (cbbPharmacy.Text.Trim().Equals(""))
                {
                    CommFun._messageBoxShow("请先确定药房名称！");
                    txtSupervisionCode.Text = "";
                    txtSupervisionCode.Focus();
                    return;
                }
                if (txtSupervisionCode.Text.Trim().Length != 20)
                {
                    CommFun._messageBoxShow("电监编码长度不合法,应为20位！");
                    txtSupervisionCode.Text = "";
                    txtSupervisionCode.Focus();
                    return;
                }                
                m_strSupervisionCode = txtSupervisionCode.Text.Trim();
                _dealCode();
                txtSupervisionCode.Text = "";
                txtSupervisionCode.Focus();
            }
        }

        /// <summary>
        /// 处理编码
        /// </summary>
        private void _dealCode()
        {
            try
            {
                //电监码核对
                string strSql = "";
                strSql = string.Format("exec P_DealSupervisionCodeSplit 'Check','{0}@{1}@{2}@{3}'", m_strSupervisionCode, cbbPharmacy.Text.Trim().Split('|')[0]
                , AppInfo.m_UserInfo.m_strCode, AppInfo.m_UserInfo.m_strName);
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    string strResult = dt.Rows[0]["Result"].ToString();
                    // 未核对成功
                    if (strResult == "0")
                    {
                        lblHint.Text = string.Format("校对提示：{0}", dt.Rows[0]["Message"].ToString());
                    }
                    else if (strResult == "2")
                    {
                        if (CommFun._messageBoxShow("该电监码未对码，是否现在对码？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                        {
                            frmGoodsCodeCheck frm = new frmGoodsCodeCheck();
                            frm.m_dictInfo.Add("Code", m_strSupervisionCode);
                            frm.m_dictInfo.Add("Name", dt.Rows[0]["Name"].ToString());
                            frm.m_dictInfo.Add("PackageSpec", dt.Rows[0]["PackageSpec"].ToString());
                            frm.m_dictInfo.Add("Unit", dt.Rows[0]["Unit"].ToString());
                            frm.m_dictInfo.Add("Manufacturer", dt.Rows[0]["Manufacturer"].ToString());
                            if (frm.ShowDialog() != DialogResult.OK)
                            {
                                return;
                            }
                            else
                            {
                                _dealCode();
                                return;
                            }
                        }
                    }
                    else
                    {
                        lblHint.Text = string.Format("校对提示：{0}", "采集正常");
                        // 显示商品信息
                        txtCode.Text = dt.Rows[0]["Code"].ToString();                        
                        txtName.Text = dt.Rows[0]["Name"].ToString();
                        txtPackageSpec.Text = dt.Rows[0]["PackageSpec"].ToString();
                        txtBatchNumber.Text = dt.Rows[0]["BatchNumber"].ToString();
                        txtManufacturer.Text = dt.Rows[0]["Manufacturer"].ToString();
                        txtMakeDate.Text = dt.Rows[0]["MakeDate"].ToString();
                        txtExpiredDate.Text = dt.Rows[0]["ExpiredDate"].ToString();
                        // 保存数据
                        _saveCode();
                        return;
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 处理编码
        /// </summary>
        private void _saveCode()
        {
            try
            {
                //电监码保存
                string strSql = "";
                strSql = string.Format("exec P_DealSupervisionCodeSplit 'Save','{0}@{1}@{2}@{3}'", m_strSupervisionCode, cbbPharmacy.Text.Trim().Split('|')[0]
                , AppInfo.m_UserInfo.m_strCode, AppInfo.m_UserInfo.m_strName);
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    m_bIsRefresh = true;
                    txtCode.Text = dt.Rows[0]["Code"].ToString();
                    txtSupervisionCode0.Text = dt.Rows[0]["SupervisionCode"].ToString();
                    txtName.Text = dt.Rows[0]["Name"].ToString();
                    txtPackageSpec.Text = dt.Rows[0]["PackageSpec"].ToString();
                    txtBatchNumber.Text = dt.Rows[0]["BatchNumber"].ToString();
                    txtManufacturer.Text = dt.Rows[0]["Manufacturer"].ToString();
                    txtMakeDate.Text = dt.Rows[0]["MakeDate"].ToString();
                    txtExpiredDate.Text = dt.Rows[0]["ExpiredDate"].ToString();
                }
            }
            catch { }
        }

        /// <summary>
        /// 处理编码
        /// </summary>
        private void _init()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtPackageSpec.Text = "";
            txtBatchNumber.Text = "";
            txtManufacturer.Text = "";
            txtMakeDate.Text = "";
            txtExpiredDate.Text = "";
        }

        private void frmConfirmSupervisionCode_Load(object sender, EventArgs e)
        {
            // 药房信息
            string strSql = "select PharmacyCode + '|' + PharmacyName as Supplier from TH_PharmacyStockOut group by PharmacyCode,PharmacyName";
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                FunctionComm._setInfoToCbb_Dt(cbbPharmacy, dt);                
                if (!AppConfig.m_strPharmacyCode.Equals(""))
                {
                    cbbPharmacy.SelectedIndex = cbbPharmacy.Items.IndexOf(AppConfig.m_strPharmacyCode);
                }
                else {
                    cbbPharmacy.SelectedIndex = 0;
                }
            }
            cbbPharmacy.DrawItem += new System.Windows.Forms.DrawItemEventHandler(CommFun._cbbDrawItem);
            txtSupervisionCode.Focus();
        }

        private void cbbPharmacy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbPharmacy.SelectedIndex >= 0)
            {
                AppConfig.m_strPharmacyCode = cbbPharmacy.Text;
                AppConfig.m_iniSetup._writeValue("Common", "PharmacyCode", AppConfig.m_strPharmacyCode);
            }            
        }

        private void frmConfirmSupervisionCode_Shown(object sender, EventArgs e)
        {
            txtSupervisionCode.Focus();
        }
    }
}